﻿Imports DTO
Imports BUS

Public Class frmDuLieuQuet

    Private irow As Integer = -1
    Private icol As Integer = -1

    Private manv As String = ""
    Private ngay As DateTime
    Private gio As String = ""

    Private oldNode As New TreeNode

    Public dsDsDuLieuQuet As New DataTable

    Private Sub CreateTree()

        trvnhanvien.Nodes.Clear()

        Dim title = "Company"
        Dim tn As New TreeNode(title)
        tn.Tag = Nothing
        trvnhanvien.Nodes.Add(tn)
        tn.ExpandAll()

        Dim dtBP As New DataTable
        dtBP = BoPhanBus.LayDanhSach()

        For Each rowbp As DataRow In dtBP.Rows
            Dim nodeBp As New TreeNode(rowbp("tenbp").ToString, 0, 0)
            nodeBp.Name = rowbp("mabp").ToString
            nodeBp.Tag = Nothing
            tn.Nodes.Add(nodeBp)

            Dim sql As String = "select manv, tennv, gioitinh from nhanvien where mabp = " & rowbp("mabp").ToString
            Dim dtNV As New DataTable
            dtNV = ProcFuncBus.ExecuteFuncSQL(sql)

            For Each rownv As DataRow In dtNV.Rows
                Dim _gt As Single

                If rownv("gioitinh") = True Then
                    _gt = 1
                Else
                    _gt = 2
                End If

                Dim nodeNv As New TreeNode(rownv("manv").ToString & ":" & rownv("tennv").ToString, _gt, _gt)
                nodeNv.Name = rownv("manv").ToString
                nodeBp.Tag = Nothing
                nodeBp.Nodes.Add(nodeNv)
            Next
        Next

        trvnhanvien.Sort()
        'trvnhanvien.ExpandAll()

    End Sub

    Private Sub LayDuQuetNV()
        Try
            Dim selNode As New TreeNode
            selNode = trvnhanvien.SelectedNode
            selNode.BackColor = Color.LightSkyBlue

            oldNode = selNode

            Dim sql As String = "exec sp_laydulieuquettheonhanvien N'" & selNode.Name &
                "', " & nudthang.Value & ", " & nudnam.Value

            dsDsDuLieuQuet = ProcFuncBus.ExecuteFuncSQL(sql)
            dgvdulieuquet.DataSource = dsDsDuLieuQuet

            If dgvdulieuquet.RowCount > 0 Then
                dgvdulieuquet.Rows(0).Cells(0).Selected = False
                'manv = trvnhanvien.SelectedNode.Name
                'ngay = String.Format("{0:yyyy-MM-dd}", dgvdulieuquet.Rows(0).Cells(0).Value)
            End If
        Catch ex As Exception
            Return
        End Try

    End Sub

    Private Sub frmDuLieuQuet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateTree()
        nudthang.Value = Date.Now.Month
        nudnam.Value = Date.Now.Year
    End Sub

    Private Sub trvnhanvien_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvnhanvien.AfterSelect
        oldNode.BackColor = Color.Transparent
        LayDuQuetNV()
    End Sub

    Private Sub nudthang_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudthang.ValueChanged
        LayDuQuetNV()
    End Sub

    Private Sub nudnam_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudnam.ValueChanged
        LayDuQuetNV()
    End Sub

    Private Sub dgvdulieuquet_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvdulieuquet.CellClick
        irow = e.RowIndex
        icol = e.ColumnIndex

        If irow < 0 Then
            Return
        Else
            Dim selNode As New TreeNode
            selNode = trvnhanvien.SelectedNode

            manv = selNode.Name
            ngay = dgvdulieuquet.Rows(irow).Cells(0).Value
            gio = dgvdulieuquet.Rows(irow).Cells(icol).Value.ToString()
        End If

    End Sub

    Private Sub trvnhanvien_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles trvnhanvien.MouseDown
        Dim selNode As New TreeNode
        selNode = trvnhanvien.SelectedNode

    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click

        If irow = -1 Then
            Return
        End If

        Dim frm As New frmThemQuetThe
        frm._manv = manv
        frm._ngay = ngay
        frm.Tag = dsDsDuLieuQuet
        frm.ShowDialog()
        'LayDuQuetNV()
        'dgvdulieuquet.Rows(irow).Cells(icol).Selected = True
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        If irow = -1 Then
            Return
        Else
            If gio = "" Then
                Return
            Else
                If icol > 0 Then
                    Dim res As MsgBoxResult = MsgBox("Ban co muon xoa", MsgBoxStyle.YesNo, "Thong bao")
                    Select Case res
                        Case MsgBoxResult.Yes
                            Dim sql As String = "delete from DanhSachQuetThe where manv = N'" & manv & "' and ngay = '" & String.Format("{0:yyyy-MM-dd}", ngay) & "' and gio = '" & gio & "'"
                            ProcFuncBus.ExecuteProcSQL(sql)

                            sql = "delete from DanhSachQuenQuetThe where manv = N'" & manv & "' and ngay = '" & String.Format("{0:yyyy-MM-dd}", ngay) & "' and gio = '" & gio & "'"
                            ProcFuncBus.ExecuteProcSQL(sql)

                            sql = "exec sp_congtheongay N'" & manv & "', '" & String.Format("{0:yyyy-MM-dd}", ngay) & "'"

                            ProcFuncBus.ExecuteProcSQL(sql)

                            For i As Integer = icol To 9 Step 1
                                If dgvdulieuquet.Rows(irow).Cells(icol + 1).Value = "" Then
                                    dgvdulieuquet.Rows(irow).Cells(icol).Value = ""
                                    Exit For
                                Else
                                    dgvdulieuquet.Rows(irow).Cells(icol).Value = dgvdulieuquet.Rows(irow).Cells(icol + 1).Value
                                End If
                            Next
                            

                            MessageBox.Show("Da xoa du lieu")

                        Case MsgBoxResult.No
                            Return
                    End Select
                Else
                    Return
                End If
            End If
        End If
    End Sub
End Class